
from unisdk.sms import UniSMS
from unisdk.exception import UniException

# 初始化
client = UniSMS("n2BNbBdnABrQuPaHAK4bdFeGtAykzWQ86JXVGBft2SeNXc1zK", "XN12G8j9RSkjgQgDGjuw1HdRAaF5JG")
def send_code(mobile,code):
  try:
    # 发送短信
    res = client.send({
      "to": str(mobile),
      "signature": "小王学python",
      "templateId": "pub_verif_login_ttl",
      "templateData": {
        "code": code,
        "ttl": 1,
      }
    })
    status = res.data["messages"][0]["status"]
    print(status)
    if status == "sent":
      return True
    else:
      return False
  except UniException as e:
    print(e)

  #{'currency': 'CNY', 'recipients': 1, 'messageCount': 1, 'totalAmount': '0.040000', 'payAmount': '0.040000', 'virtualAmount': '0', 'messages': [{'id': 'ec67295ba63940f5cca7b9bc8e1239bb', 'to': '+8613183066336', 'regionCode': 'CN', 'countryCode': '86', 'messageCount': 1, 'status': 'sent', 'upstream': 'bjjx.vfy.standard', 'price': '0.040000'}]}
if __name__ == '__main__':
    send_code(13183066336,1258)